/**
 * 
 */
package com.roc.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.roc.dao.DaoException;
import com.roc.dao.ISQLBaseDao;

/**
 * @author Administrator
 * 
 */
@Repository("sqlBaseDao")
public class SQLBaseDaoImpl implements ISQLBaseDao {

	static Logger logger = LogManager.getLogger(SQLBaseDaoImpl.class);

	@Autowired
	protected SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.dao.ISQLBaseDao#querySqlToMap(java.lang.String)
	 */
	public List<Map<String, Object>> querySqlToMap(String sql) throws DaoException {
		try {
			Session session = sessionFactory.getCurrentSession();
			SQLQuery query = session.createSQLQuery(sql);
			query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			List<Map<String, Object>> list = query.list();
			return list;
		} catch (Exception e) {
			throw new DaoException("查询SQL语句失败[" + sql + "]", e);
		}

	}

	@Override
	public int deleteBySql(String sql, List<Object> parmas)
			throws DaoException {
		int total=0;
		try {
			Session session = sessionFactory.getCurrentSession();
			SQLQuery query = session.createSQLQuery(sql);
			if(parmas!=null&&parmas.size()>0){
				for(int i=0;i<parmas.size();i++){
					query.setParameter(i, parmas.get(i));
				}
				
			}
			total=query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DaoException("删除SQL语句失败[" + sql + "]", e);
		}
		return total;
	}
	@Override
	public int removeBySql(String sql,Object[] parmas)
			throws DaoException {
		int total=0;
		try {
			Session session = sessionFactory.getCurrentSession();
			SQLQuery query = session.createSQLQuery(sql);
			if(parmas!=null&&parmas.length>0){
				for(int i=0;i<parmas.length;i++){
					query.setParameter(i, parmas[i]);
				}
				
			}
			total=query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DaoException("删除SQL语句失败[" + sql + "]", e);
		}
		return total;
	}
	@Override
	public List<Object> queryAll(String sql) throws DaoException {
		List<Object> objList=new ArrayList<Object>();
		try {
			Session session = sessionFactory.getCurrentSession();
			SQLQuery query = session.createSQLQuery(sql);
			
			objList=query.list();
		} catch (Exception e) {
			throw new DaoException("查询SQL语句失败[" + sql + "]", e);
		}
		return objList;
	}
	@Override
	public List<Object> queryAll(String sql, Object[] parmas) throws DaoException {
		List<Object> objList=new ArrayList<Object>();
		try {
			Session session = sessionFactory.getCurrentSession();
			SQLQuery query = session.createSQLQuery(sql);
			if(parmas!=null&&parmas.length>0){
				for(int i=0;i<parmas.length;i++){
					query.setParameter(i, parmas[i]);
				}
				
			}
			objList=query.list();
		} catch (Exception e) {
			throw new DaoException("查询SQL语句失败[" + sql + "]", e);
		}
		return objList;
	}
	@Override
	public int executeBySql(String sql)
			throws DaoException {
		int total=0;
		try {
			Session session = sessionFactory.getCurrentSession();
			SQLQuery query = session.createSQLQuery(sql);
			
			total=query.executeUpdate();
		} catch (Exception e) {
			throw new DaoException("执行SQL语句失败[" + sql + "]", e);
		}
		return total;
	}
	
	 
	public Object findObj(String sql,Object[] parmas) throws DaoException {
		Object  obj=null;
		try {
			Session session = sessionFactory.getCurrentSession();
			SQLQuery query = session.createSQLQuery(sql);
			if(parmas!=null&&parmas.length>0){
				for(int i=0;i<parmas.length;i++){
					query.setParameter(i, parmas[i]);
				}
				
			}
			obj=query.uniqueResult();
		} catch (Exception e) {
			throw new DaoException("执行SQL语句失败[" + sql + "]", e);
		}
		return obj;
	}

	
}
